package com.fzg.common.demo;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * create by fzg
 * 2022/11/1 16:32
 * 发送QQ邮件demo
 */


public class MailDemo01 {

    public static void main(String[] args) throws MessagingException {
        // 与真正邮箱建立连接
        Properties props = new Properties();
        props.put("main.transport.protocol","smtp");
        props.put("mail.smtp.host","smtp.qq.com");
        Session session = Session.getInstance(props);
        // 创建虚拟邮件对象
        MimeMessage message = new MimeMessage(session);
        // 发送人
        message.setFrom(new InternetAddress("1840633803@qq.com"));
        // 接收人
        message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress("255394562@qq.com"));
        // 主题
        message.setSubject("早");
        // 正文
        message.setText("早安！");
        message.saveChanges();
        // 创建一个发送者
        Transport transport = session.getTransport();
        // 得到一个邮箱认证
        transport.connect("范占国","fvcqvilihpfkfbhh");
        transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }

}
